package com.why.jvm.hotspot.src.share.vm.oops;


import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoTian
 * @date 2022/7/16
 * @Description:
 * Klass 表示 Java类在 JVM 中的存在形式
 *  - InstanceKlass 表示类的原信息
 *      - InstanceMirrorKlass 表示类的 java.lang.Class,存储在堆区，叫做镜像类
 *      - InstanceRefKlass 表示 java.lang.ref.Reference 的子类
 *      - InstanceCLassLoaderKlass 用于遍历某个加载器加载的类
 *  - ArrayKlass 表示数组类的原信息
 *      - TypeArrayKlass 表示基本类型的数组的原信息
 *      - ObjArrayKlass 表示引用类型的的数组的原信息
 */
@Data
public class InstanceKlass extends Klass{

    private String magic;
    private int minorVersion;
    private int majorVersion;
    private ConstantPool constantPool;

    private int accessFlag;
    private int thisClass;
    private int superClass;

    private int interfaceCount;
    private List<InterfaceInfo> interfaceInfos = new ArrayList<>();

    private int fieldCount;
    private List<FieldInfo> fieldInfos = new ArrayList<>();

    private int methodCount;
    private List<MethodInfo> methodInfos = new ArrayList<>();

    private int attributeCount;
    private List<AttributeInfo> attributeInfos = new ArrayList<>();

    @Override
    public String toString() {
        StringBuffer r =  new StringBuffer("InstanceKlass{" + '\n'+
                "magic='" + magic + '\'' + '\n'+
                "minorVersion=" + minorVersion +'\n'+
                "majorVersion=" + majorVersion +'\n');
        for (int i = 1; i < constantPool.getCount(); i++) {
            r.append("第["+i+"]个常量"+constantPool.getConstantPoolMap().get(i).toString() +"\n");
        }
        r.append(
                "accessFlag=" + accessFlag +'\n'+
                "thisClass=" + thisClass +'\n'+
                "superClass=" + superClass +'\n'+
                "interfaceCount=" + interfaceCount +'\n'
        );

        for (int i = 0; i < interfaceCount; i++) {
            r.append("第["+i+"]个接口"+interfaceInfos.get(i).toString()+'\n');
        }
        r.append(
                "fieldCount=" + fieldCount +'\n');
        for (int i = 0; i < fieldCount; i++) {
            r.append("第["+i+"]个字段"+fieldInfos.get(i).toString()+'\n');
        }
        r.append(
                "methodCount=" + methodCount +'\n');
        for (int i = 0; i < methodCount; i++) {
            r.append("第["+i+"]个方法"+methodInfos.get(i).toString()+'\n');
        }
        r.append(
                "attributeCount=" + attributeCount +'\n');
        for (int i = 0; i < attributeCount; i++) {
            r.append("第["+i+"]个属性"+attributeInfos.get(i).toString()+'\n');
        }
        return r.toString();
    }
}
